package scala.test

import scala.io.Source

/**
  * 统计学生的总分
 */
object Test02 {
  def main(args:Array[String]):Unit = {
    //读数据
    val scores: List[String] = Source.fromFile("data/score.txt").getLines().toList
    //取数据
    val idAndscore: List[(String, Int)] = scores.map((data:String)=>{
      val split: Array[String] = data.split(",")
      val id: String = split(0)
      val score: Int = split(2).toInt
      (id,score)
    })
    //根据学生id分组
    val group: Map[String, List[(String, Int)]] = idAndscore.groupBy((kv:(String,Int))=>kv._1)
    //求学生的总分
    val idAndScoreSum: Map[String, Int] = group.map((kv:(String,List[(String,Int)]))=>{
      val id: String = kv._1
      val scores: List[(String, Int)] = kv._2
      //还要在取一次
     val scoreNum: List[Int] =  scores.map((f:(String,Int)) =>{
        val score: Int = f._2
        score
      })
      val scoreSum: Int = scoreNum.sum
      (id,scoreSum)
    })
    val tuples: List[(String, Int)] = idAndScoreSum.toList.sortBy(t=>t._1)
    tuples.foreach(println)
  }
}
